package com.honghu.villagepost.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CheckBox;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.honghu.villagepost.R;
import com.honghu.villagepost.base.BaseActivity;
import com.honghu.villagepost.constant.SputilsConstant;
import com.honghu.villagepost.util.WebViewOB;

/**
 * Desc:商家协议
 * <p>
 * Author: [李豫]
 * Date: 2021-05-10
 * Copyright: Copyright (c) 2010-2021
 * Company: @咪咕动漫
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class ShopProtocolActivity extends BaseActivity {

    private WebView mWebView;
    private CheckBox mAgreeCb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_protocol;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("商家入驻");

        mWebView = findViewById(R.id.webView);
        mAgreeCb = findViewById(R.id.protocol_cb);
        findViewById(R.id.next_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextConfirm();
            }
        });

        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    view.loadUrl(url);
                } else {
                    try {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                    } catch (Exception e) {
                    }
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

        // 设置允许加载混合内容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        mWebView.loadUrl("file:///android_asset/shop_apply_protocol.html");
        mWebView.addJavascriptInterface(new WebViewOB(this), "android");
        mWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {  //表示按返回键
                        mWebView.goBack();   //后退
                        return true;    //已处理
                    }
                }
                return false;
            }
        });
    }

    private void nextConfirm() {
        if (mAgreeCb.isChecked()){
            startActivity(new Intent(this, ShopApplyActivity.class));
            finish();
        }else {
            ToastUtils.showShort("请阅读并同意用户入驻须知");
        }
    }
}